﻿using System.Collections.Generic;
using PlayMyLife.Definitions.HumanProperty;
using PlayMyLife.ModelInterface;

namespace PlayMyLife.ModelLocal {
    public class LocalRolesManager : IRolesManager {
        public HumanSelfData GetRoleData(long id) {
            if(roles.TryGetValue(id, out var ret)) {
                return ret;
            }
            return null;
        }

        public bool AddRoleData(HumanSelfData data) {
            if(roles.ContainsKey(data.id)) {
                return false;
            }
            roles.Add(data.id, data);
            return true;
        }

        public RelationData GetRelation(long playerA, long playerB) {
            if(relations.ContainsKey(playerA) && relations.ContainsKey(playerB)) {
                var pARelations = relations[playerA];
                if(pARelations.TryGetValue(playerB, out var ret)) {
                    return ret;
                }
            }
            return null;
        }

        public bool ChangeRelation(RelationData relation) {
            if(!relations.ContainsKey(relation.srcId)) {
                relations.Add(relation.srcId, new Dictionary<long, RelationData>());
            }
            var srcRelations = relations[relation.srcId];
            if(srcRelations.ContainsKey(relation.destId)) {
                srcRelations.Remove(relation.destId);
            }
            srcRelations.Add(relation.destId, relation);
            if(!relations.ContainsKey(relation.destId)) {
                relations.Add(relation.destId, new Dictionary<long, RelationData>());
            }
            var destRelations = relations[relation.destId];
            if(destRelations.ContainsKey(relation.srcId)) {
                destRelations.Remove(relation.srcId);
            }
            destRelations.Add(relation.srcId, relation);
            return true;
        }

        private readonly Dictionary<long, HumanSelfData> roles = new();
        private readonly Dictionary<long, Dictionary<long, RelationData>> relations = new();
    }
}